var _uri = 'report_into_stockholding_inquiry';
$(function() {
	initDP();
	initCheckbox_box_w(0);
});
function proCodeQuery(){
	var pro_code_query = $.trim($('#pro_code_query').val());
	var idx = 0;
	postHandler(_uri, {fun:query_arr[idx],'pro_code_query':pro_code_query}, function(val){
		initPopup(val, idx);
		initPopupWindow();
		setTimeout('initChkbox()',500);
	});
}
var query_arr = ['pro_code','brand_name','storage_name','Custom_Att_4'];
var query_cont_arr = [0,0,0];
function initCheckbox_box_w(idx){
	if(idx<query_arr.length)
	postHandler(_uri, {fun:query_arr[idx]}, function(val){
		initPopup(val, idx);
		if(idx==query_arr.length-1){
			initPopupWindow();
			setTimeout('initChkbox()',500);
		}else
			initCheckbox_box_w(idx+1);
	});
}
function initPopup(val, num){
	var oWin = $('.box_'+num);
	var html = '<div><label><input name="checkbox" type="checkbox" id="" checked="checked"><strong style="color:#00aacc;">全部</strong></label></div><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk1('+num+')" type="button" value="确定" />';
	oWin.html(html);
	var div = oWin.find('.checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	var count = 0;
	if(val.length==0){
		$('.box_'+num).html('<div>无资料</div>');
		query_cont_arr[num] = 0;
	}
	for(var i=0;val.length>0 && i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked"><strong>'+t1+'</strong></label>';
		if(tok2.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var t2 = tok3[j];
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked">'+t2+'</label></div>';
				idx++;
				count++;
			}
			_html += '</div>';
		}else 
			count ++;
		_div.html(_html).appendTo(div);
		query_cont_arr[num] = count;
	}
}
function chkboxOk(num){
	var oTar = $('.box_'+num);
	var clone = oTar.clone();
	var oChks = clone.find('input:checkbox');
	oTar.find('input:checkbox').each(function(idx){
		var chk = $(this)[0].checked;
		var oDiv = oChks.eq(idx).closest('div');
		if(oDiv.parent().hasClass('checkbox_box_w')) oDiv = oDiv.parent();
		if(chk==false){
			oDiv.remove();
		}else
			oChks.eq(idx).remove();
		if(idx==(oChks.length-1)){
			$('.show_'+num).html('<div class="checkbox_box_w">'+clone.find('.checkbox_box:eq(0)').html()+'</div>');
			clone.remove();
		}
	});
}
function chkboxOk1(num){
	var oTar = $('.box_'+num);
	var clone = oTar.clone();
	var oChks = clone.find('input:checkbox');
        var len=oChks.length;
        for(var idx=0; idx<len; idx++){
                var obj=oChks[idx];
                var chk = $(obj)[0].checked;
		var oDiv = oChks.eq(idx).closest('div');
		if(oDiv.parent().hasClass('checkbox_box_w')) oDiv = oDiv.parent();
		if(chk==false){
			oDiv.remove();
		}else{
			oChks.eq(idx).remove();
                }
		if(idx==(len-1)){
			$('.show_'+num).html('<div class="checkbox_box_w">'+clone.find('.checkbox_box:eq(0)').html()+'</div>');
			clone.remove();
		}
        }
        
}
function query(){
	if($.trim($('#begin_time').val()).length>0 && !isDateStr($('#begin_time').val())){
		alert('开始日期有误');$('#begin_time').focus();return;
	}else if($.trim($('#end_time').val()).length>0 && !isDateStr($('#end_time').val())){
		alert('结束日期有误');$('#end_time').focus();return;
	}else if(compareDateStr($('#begin_time').val(), $('#end_time').val())<0){
		alert('结束日期应大于开始日期');$('#end_time').focus();return;
	}
	var datas = {fun:'query'};
	for(var i=0;i<query_arr.length;i++){
		var clone = $('.show_'+i).clone();
		var oLab = clone.find('label');
		for(var j=0;j<oLab.length;j++){
			if(oLab.eq(j).attr('hasChildren')) oLab.eq(j).html('');
		}
		str = $.trim(clone.html().replace(/<.*?>/g,' ').replace(/[\r\n]/g,' ')).replace(/\s+/g,'_###_');
		clone.remove();
		//if(str.length>0 && str.split('_###_').length<query_cont_arr[i])
		if(str.length>0)datas[query_arr[i]] = str;
	}
	var oIpts = $('input[type="text"]');
	for(var i=0;i<oIpts.length;i++){
		if($.trim(oIpts.eq(i).val()).length>0)
			if(oIpts.eq(i)[0] && $.trim(oIpts.eq(i).val()).length>0)
				datas[oIpts.eq(i)[0].id] = oIpts.eq(i).val();
	}
	postHandler(_uri, datas, function(val){
		window.location='report_into_stockholding.html';
		return false;
	});
}

function initDP(){
	$(".dpicker").datepicker({dateFormat: 'yy/mm/dd',showButtonPanel: true,changeMonth: true, changeYear: true, yearRange: '-10:+0', showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.png'});
	/*
	$('#Custom_Att_4').datepicker({dateFormat: 'yy',stepMonths: 12,showButtonPanel: true,maxDate:0,minDate:-10*365,currentText: '今年',beforeShow:doBeforeShow,onChangeMonthYear:changeMonthYear, changeMonth: true,changeYear: true, yearRange: '-10:+0', showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.png'})
		.focus(function(){$("#ui-datepicker-div table, select.ui-datepicker-month").hide();});
	*/
}
function doBeforeShow(input, inst){
	var height = $(input).height();
	var offset = $(input).offset();
	window.setTimeout(function(){
        inst.dpDiv.css({top:(offset.top+height+4)+'px',left: offset.left+'px'});
    },1);
}
function changeMonthYear(year, month, inst){
	$('#'+inst.id).val(year);
}